function [xexpect,cdflowerb,cdfupperb]=norm_trexpect(mu,sigma,lowerb,upperb) 
% ======================================================================== 
% function norm_trexpect(mu,sigma,lowerb,upperb) 
%
% This function computes the expectation of a two sided truncated normal 
% x~N( mu, sigma ) 
% where lowerb < x < upperb 
% Then the expectation is given by 
% E( x | a < x < b ) = mu + sigma*[ (phi(za)-phi(zb))/(PHI(zb)-PHI(za)]
% where za=(lowerb-mu)/sigma 
%       zb=(upperb-mu)/sigma 
%       phi=pdf of the standardized normal (0,1) 
%       PHI=cdf of the standardized normal (0,1) 
% 
% Output 
% ------
% XEXPECT       Expected value of truncated normal 
% CDFLOWERB     PHI(za)
% CDFUPPERERB   PHI(zb) 
%               
% Recall  pdf(x)=sigma*phi(z)/[PHI(zb)-PHI(za)] 
% Hence denominator can be used to easily compute 
% a conditional pdf if needed 
%
% Alejandro Justiniano March 23 2010 
% =========================================================================
cdflowerb=normcdf((lowerb-mu)/sigma,0,1); 
cdfupperb=normcdf((upperb-mu)/sigma,0,1); 
xexpect=mu+( sigma*( normpdf( (lowerb-mu)/sigma ,0,1 )-normpdf( (upperb-mu)/sigma ,0,1 ) )/(cdfupperb-cdflowerb)); 